//
//  DNCryingManager.m
//  DanoneEA
//
//  Created by Senela on 16/10/18.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import "DNCryingManager.h"
#import "DNCryingDao.h"


@implementation DNCryingManager

#pragma mark - Public methods

+ (instancetype)sharedInstance {
    static DNCryingManager *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}


-(BOOL)saveDairyToLocalDB:(DNCryingModel *)diaryInfo
{
    BOOL ret = NO;
    
    DNCryingDao *dao = [[DNCryingDao alloc] init];
    ret = [dao saveCryingModel:diaryInfo];

    return ret;
}

/**
 *  save crying dairy list in local db
 *
 *  @param diaryArr
 *
 *  @return YES/NO
 */
-(BOOL)saveCryingDairyArrToLocalDB:(NSArray *)diaryArr
{
    DNCryingDao *dao = [[DNCryingDao alloc] init];
    BOOL ret = [dao saveCryingModelWithArray:diaryArr];
    
    return ret;
}


-(BOOL)deleteDairyFromLocalDBByAppId:(NSString *)appId
{
    BOOL ret = NO;
    
    DNCryingDao *dao = [[DNCryingDao alloc] init];
    ret = [dao deleteCryingModelByAppId:appId];
    
    return ret;
}

-(BOOL)isOverlapInLocalDB:(DNCryingModel *)diaryInfo
{
    BOOL ret = NO;
     
    DNCryingDao *dao = [[DNCryingDao alloc] init];
    ret = [dao iSOverlapInDBCurrentCryingModel:diaryInfo];

    return ret;
}

-(void)synchronousCryingData
{
    
//                      NSDate *lastFourDate =[self getLastFourDate];
                      
                      DNCryingDao *dao = [[DNCryingDao alloc] init];
                      
                       //get All Local crying data witch status is  OPStatus_ADD/OPStatus_MODIFY/OPStatus_DELETE From local DB by lastFourDate
                       NSMutableArray *localCryingArr = [dao getAllObjectModelNotSynchronized];
                       
                       if(localCryingArr.count == 0)
                        return;
                       
                      NSArray *dictArray = [DNCryingModel mj_keyValuesArrayWithObjectArray:localCryingArr];
                      
                      //TODO: upload data to application server and overwrite local data if succussed
                      /*
                       1、upload dictArray data to application server
                       2、if failed， do nothing
                       3、if succussed，update localCryingArr by serverId and status[OPStatus_NORMAL]
                       4、save diaryArr info in local DB
                       */
                      [self saveDairyArrToLolcalDB:localCryingArr];
                        
}

#pragma mark - Private methods
/**
 *  save diaryArr info in local DB without any modify
 *
 *  @param diaryArr <DNCryingModel>
 *
 *  @return YES/NO
 */
-(BOOL)saveDairyArrToLolcalDB:(NSArray*)diaryArr
{
    BOOL ret = NO;
    //TODO:save diaryArr info in local DB without any modify
    
    DNCryingDao *dao = [[DNCryingDao alloc] init];
    ret = [dao saveCryingModelWithArray:diaryArr];
    
    return ret;
}


@end
